package com.shop.shop_end.handle;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
//WebMvcConfigurationSupport
//定制SpringMVC的一些功能都使用WebMvcConfigurer
public class InterceptorConfig implements WebMvcConfigurer {
    @Resource
    private LoginInterceptor loginInterceptor;
    /**
     * 配置拦截器
     * @param registry 相当于拦截器的注册中心
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] excludePathList = {"/",
                "/user/registerPage", "/user/loginPage", "/user/index",
                "/user/register", "/user/login", "/user/exit",
                "/static/css/**", "/static/js/**", "/static/images/**",
                "/static/pic/**", "/file/download/**", "/upfiles/**",
                "/admin/registerPage", "/admin/loginPage",
                "/admin/register", "/admin/login", "/admin/main", "/admin/exit"
        };
//       下面这句代码相当于添加一个拦截器   添加的拦截器就是我们刚刚创建的
        registry.addInterceptor(loginInterceptor)//LoginInterceptor是我们设置拦截器规则
//       addPathPatterns()配置我们要拦截哪些路径 addPathPatterns("/**")表示拦截所有请求，包括我们的静态资源
                 .addPathPatterns("/**")
//       excludePathPatterns()表示我们要放行哪些（表示不用经过拦截器）
//       excludePathPatterns("/","/public/login")表示放行“/”与“/public/login”请求
//       如果有静态资源的时候可以在这个地方放行
                 .excludePathPatterns(excludePathList);//排除/static目录及其子目录下静态文件的拦截
//         super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/WEB-INF/views/**").addResourceLocations("classpath:/WEB-INF/views/");
        registry.addResourceHandler("/upfiles/**").addResourceLocations("classpath:/upfiles/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/user/index").setViewName("/user/index");
        registry.addViewController("/user/loginPage").setViewName("/user/login");
        registry.addViewController("/user/registerPage").setViewName("/user/register");
        registry.addViewController("/admin/registerPage").setViewName("/admin/register");
        registry.addViewController("/admin/loginPage").setViewName("/admin/login");
    }

//    @Bean
//    public LoginInterceptor loginInterceptor(){
//        return new LoginInterceptor();
//    }
}
